﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Database;
using System.Data;
using ShoppingCart;

public partial class accessoriedetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["tenkh"] == null)
        {
            Panel_binhluan.Enabled = false;
            lb_User.Text = "Vui lòng đăng nhập trước khi bình luận";
        }
        else
        {
            Panel_binhluan.Enabled = true;
            lb_User.Visible = false;
        }
        if (!IsPostBack)
        {
            if (Session["Gio_hang"]!= null)Cache["Gio_hang"] = Session["Gio_hang"];
            if (Request["man"] != null)
            {
                string mamay = Request.QueryString["man"].ToString();
                showDetail(mamay);
                showCommnent(mamay);
            }

        }
    }
    static int tong = 0;
    void showCommnent(string mamay)
    {
        BinhLuan bl = new BinhLuan();
        int ma = Convert.ToInt32(mamay);
        DataTable table = bl.DanhSachBinhLuan(ma);
        dl_binhluan.DataSource = table;
        dl_binhluan.DataBind();
        tong = bl.LayTongBinhLuan(ma);
        if (table.Rows.Count > 0)
        {
            lb_dembinhluan.Text = "Có:" + tong.ToString() + "bình luận";
        }
        else
        {
            lb_dembinhluan.Text = "Không có bình luận nào";
        }

    }


    void showDetail(string mammay)
    {
        LinhKien lk = new LinhKien();
        DataSet ds  = lk.getLKById(mammay);
        ddl_chitietmaytinh.DataSource = ds;//lk.getLKById(mammay);
        ddl_chitietmaytinh.DataBind();
        lb_tensanphambinhluan.Text = ds.Tables[0].Rows[0]["tenlinhkien"].ToString();
    }
    protected void btn_gui_Click(object sender, EventArgs e)
    {
        BinhLuan bl = new BinhLuan();
        string mamay = Request.QueryString["man"].ToString();
        bl.Mamay = int.Parse(Request.QueryString["man"].ToString());
        bl.Tieude = txt_tieude.Text;
        bl.Noidung = txt_comment.Text;
        bl.Hoten = Session["tenkh"].ToString();
        bl.Ngaydang = DateTime.Now;
        bl.ThemBinhLuan(bl);
        showCommnent(mamay);
    }

    protected void ddl_chitietsanpham_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Mua")
        {
            string ma = e.CommandArgument.ToString();
            LinhKien lk = new LinhKien();
            DataSet product = lk.getLKById(ma);
            Item item = new Item();
            foreach (DataRow row in product.Tables[0].Rows)
            {
                item.ID = Convert.ToInt32(row["malinhkien"].ToString());
                item.Name = row["tenlinhkien"].ToString();
                item.Image = row["hinhanh"].ToString();
                item.Cost = Convert.ToDouble(row["giaban"].ToString());
                item.Quantity = 1;
                item.Total = Convert.ToDouble(row["giaban"].ToString());

            }
            if (Session["ShoppingCart"] == null)
            {
                Session["ShoppingCart"] = new ShoppingCart.Cart();
            }

            ShoppingCart.Cart cart = (ShoppingCart.Cart)Session["ShoppingCart"];
            cart.AddToCart(item);
            Session["ShoppingCart"] = cart;
            string links = Request.ServerVariables["HTTP_REFERER"].ToString();
            Response.Redirect(links);
        }
    }
    protected void ImageButtonGioHang_Click(object sender, ImageClickEventArgs e)
    {

    }
}